const { Controller } = require('egg');
const { ctxBodyFormatter } = require('../util/formatter');

class TeacherController extends Controller {
  async index() {
    const { ctx, service } = this;
    const res = ctxBodyFormatter(await service.teacher.getAllTeacher());
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async show() {
    const { ctx, service } = this;
    const teacher = { tId: ctx.params?.id };
    const res = ctxBodyFormatter(await service.teacher.getTeacher(teacher));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async create() {
    const { ctx, service } = this;
    const teacher = ctx.request.body;
    const res = ctxBodyFormatter(await service.teacher.appendTeacher(teacher));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async update() {
    const { ctx, service } = this;
    const teacher = ctx.request.body;
    const res = ctxBodyFormatter(await service.teacher.updateTeacher(teacher));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async destroy() {
    const { ctx, service } = this;
    const teacher = { tId: ctx.params?.id };
    const res = ctxBodyFormatter(await service.teacher.destroyTeacher(teacher));
    ctx.status = res.status;
    ctx.body = res.body;
  }
}

module.exports = TeacherController;
